package com.p2p.front.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.p2p.base.model.BaseUser;
import com.p2p.base.page.PageVo;
import com.p2p.base.util.BasePropertyConfigurer;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.dao.UserDao;
import com.p2p.front.dal.model.UserInfo;
import com.p2p.front.service.UserInfoService;
import com.p2p.front.service.UserLoanService;
import com.p2p.front.vo.UserInfoVO;
import com.p2p.user.enums.UserInfoName;

/**
 * leiwen@yiqihao.com
 */
@Controller()
@RequestMapping("/userloan")
public class UserLoanController extends BaseController{

	@Resource
	private UserLoanService userLoanService;
	
	@Resource
	private UserDao userDao;
	
	@Resource
	private UserInfoService userInfoService;

	@RequestMapping("/list")
	public String list(PageVo vo, Model model) {
		vo.getParams().put("uid", super.getCurrentUser().getId()+"");
		PageVo page = userLoanService.myLoanList(vo);
		model.addAttribute("page", page);
		return "userloan/list";
	}
	
	@RequestMapping("/torepay")
	public String torepay(PageVo vo, Model model) {
		vo.getParams().put("uid", super.getCurrentUser().getId()+"");
		PageVo page = userLoanService.myLoanList(vo);
		model.addAttribute("page", page);
		return "userloan/torepay";
	}
	
	@RequestMapping("/repayed")
	public String repayed(PageVo vo, Model model) {
		vo.getParams().put("uid", super.getCurrentUser().getId()+"");
		PageVo page = userLoanService.myLoanList(vo);
		model.addAttribute("page", page);
		return "userloan/repayed";
	}
	
	@RequestMapping("/count")
	public String count(Model model){
		Map<String,Object> map = userLoanService.count(Integer.valueOf(super.getCurrentUser().getId()));
		model.addAllAttributes(map);
		return "userloan/count";
	}
	
	@RequestMapping("/info")
	public String info(ModelMap model){
		BaseUser user = getCurrentUser();
		UserInfoVO userInfoVO = userInfoService.findByNameEnums(user.getId(),Arrays.asList(UserInfoName.values()));
		Map<String, Object> userCertInfo= userDao.get(Long.parseLong(user.getId()));
		model.addAttribute("infoMap", userInfoVO.getInfoMap());
		model.addAttribute("realname", userCertInfo.get("realname"));
		model.addAttribute("idcard", userCertInfo.get("idcard"));
		return "userloan/info";
	}
	
	@RequestMapping("/editinfo")
	public String edifPersonInfo(UserInfoVO userInfoVO,ModelMap model) {
		Map<String,String> maps= userInfoVO.getInfoMap();
		BaseUser user = getCurrentUser();
		userInfoVO.setUserId(user.getId());
		userInfoService.batchSave(userInfoVO);
		return  "redirect:/userloan/info.html";
	}
	
	//TODO 貌似没用了
	@RequestMapping("/uploadfile")
	public void uploadFile(HttpServletRequest request,HttpServletResponse response, @RequestParam("info_avatar_file")MultipartFile file,ModelMap model) throws IOException {
		if(file!=null&&!file.isEmpty()) {
			System.out.println(file.getName());
			String imagePath = (String) BasePropertyConfigurer.getContextProperty("userloan.info.png.path")  + "userloaninfo/";
			String name = file.getOriginalFilename();
			//复制文件
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(imagePath,name));
			String url = "/userinfloan/"+name;
//			model.addAttribute("result",result);
			model.addAttribute("url",url);
			JSONObject json = new JSONObject();
			json.put("result", true);
			json.put("url", url);
			this.returnJsonObject(response, json);
		}
		System.out.println("Success");
//		return "redirect:/userloan/info.html";
	}
}
